import { createStore } from 'vuex'
import BaseApi from "@/api/index.js";

export default createStore({
  state: {
    // 播放控制器信息
    playList: [{ al: {} }],
    playCurrentIndex: 0,
    // 歌词
    lyric: "",
    // 歌词时间
    currentTime: 0,
    intervalID: 0,
    // 用户信息
    user: {
      isLogin: false,
      username: "未登录"
    }
  },
  getters: {
    lyricList: function (state) {
      let arr = state.lyric.split(/\n/gis).map((item, i, arr) => {
        // 分钟、秒钟、毫秒
        let min = item.slice(1, 3);
        let sec = item.slice(4, 6);
        let mill = item.slice(7, 9);
        // 歌词
        let lyric = item.slice(10, item.length)
        // 总时间
        let time = parseInt(mill) + parseInt(sec) * 1000 + parseInt(min) * 60 * 1000
        return {
          min,
          sec,
          mill,
          lyric,
          time,
          item
        }
      });

      // 记录上一句的信息
      arr.forEach((el, i) => {
        if (arr[i + 1] && arr[i + 1].time) {
          el.next = arr[i + 1].time
        } else {
          el.next = Infinity
        }
      })
      return arr;
    },
  },
  // 修改state中的数据必须通过mutations
  mutations: {
    setPlayList(state, value) {
      state.playList = value
    },
    pushPlayList(state, value) {
      state.playList.push(value)
    },
    setPlayIndex(state, value) {
      state.playCurrentIndex = value
    },
    setLyric(state, value) {
      state.lyric = value
    },
    setCurrentTime(state, value) {
      state.currentTime = value
    },
    setUser(state, value) {
      state.user = value
    }
  },
  actions: {
    // content指store的环境，payload指参数
    // 获取歌词
    async reqLyric(content, payload) {
      let res = await BaseApi.getLyric(payload.id)
      // console.log(res.data.lrc.lyric)
      content.commit("setLyric", res.data.lrc.lyric)
    },
    // 登录
    async phoneLogin(content, payload) {
      console.log(payload)
      let res = await BaseApi.phoneLogin(payload.username, payload.password)
      console.log(res)
    }
  },
  modules: {
  }
})
